package com.frxuexi.demo01_compose.view

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Done
import androidx.compose.material.icons.filled.Email
import androidx.compose.material.icons.filled.Person
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.AssistChip
import androidx.compose.material3.AssistChipDefaults
import androidx.compose.material3.Badge
import androidx.compose.material3.BadgedBox
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ElevatedButton
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExtendedFloatingActionButton
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.FilterChip
import androidx.compose.material3.FilterChipDefaults
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.InputChip
import androidx.compose.material3.InputChipDefaults
import androidx.compose.material3.LargeFloatingActionButton
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import com.frxuexi.demo01_compose.R
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun myButton(onClick: () -> Unit) {
    Column {
        Button(
            modifier = Modifier
                .height(40.dp)
                .width(100.dp),
            onClick = { Log.i("===", "登录点击了") }) {
            Text(text = "登录")
        }
        Button(onClick = onClick) {
            Text(text = "登录")
        }
        FilledTonalButton(onClick = { Log.i("===", "注册点击了") }) {
            Text(text = "注册")
        }

        OutlinedButton(onClick = { /*TODO*/ }) {
            Text(text = "轮廓")
        }

        ElevatedButton(onClick = { /*TODO*/ }) {
            Text(text = "凸起")
        }

        TextButton(onClick = { /*TODO*/ }) {
            Text(text = "文本按钮")
        }

        FloatingActionButton(onClick = { Log.i("===", "悬浮点击了") }) {
            Icon(imageVector = Icons.Default.Add, contentDescription = "")
        }

        LargeFloatingActionButton(onClick = { /*TODO*/ }, shape = CircleShape) {
            Icon(imageVector = Icons.Default.Add, contentDescription = "")
        }

        ExtendedFloatingActionButton(onClick = { /*TODO*/ }) {
            Icon(imageVector = Icons.Default.Add, contentDescription = "")
            Text(text = "ha")
        }

        Card(
            colors = CardDefaults.cardColors(
                containerColor = MaterialTheme.colorScheme.surfaceVariant,
            ),
            modifier = Modifier
                .size(width = 240.dp, height = 100.dp)
        ) {
            Text(
                text = "Filled",
                modifier = Modifier
                    .padding(16.dp),
                textAlign = TextAlign.Center,
            )
        }

        AssistChip(
            onClick = { Log.d("Assist chip", "hello world") },
            label = { Text("Assist chip") },
            leadingIcon = {
                Icon(
                    Icons.Filled.Settings,
                    contentDescription = "Localized description",
                    Modifier.size(AssistChipDefaults.IconSize)
                )
            }
        )


        //记录是否选中
        var select by remember { mutableStateOf(false) }
        FilterChip(selected = select, onClick = {
            select = !select
        }, label = { Text(text = "选择") },
            leadingIcon = {
                if (select) {
                    Icon(imageVector = Icons.Default.Done, contentDescription = "")
                } else {
                    null
                }
            })

    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun myCom2(text: String, onDismiss: () -> Unit) {
    var enabled by remember { mutableStateOf(true) }
    if (!enabled) return

    InputChip(
        onClick = {
            onDismiss()
            enabled = !enabled
        },
        label = { Text(text) },
        selected = enabled,
        avatar = {
            Icon(
                Icons.Filled.Person,
                contentDescription = "Localized description",
                Modifier.size(InputChipDefaults.AvatarSize)
            )
        },
        trailingIcon = {
            Icon(
                Icons.Default.Close,
                contentDescription = "Localized description",
                Modifier.size(InputChipDefaults.AvatarSize)
            )
        },
    )
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MyDialog(
    openAlertDialog: MutableState<Boolean>,
    onDismissRequest: () -> Unit,
    confirmClick: () -> Unit
) {
    Column {
        Button(modifier = Modifier
            .width(120.dp)
            .height(40.dp), onClick = { openAlertDialog.value = !openAlertDialog.value }) {
            Text(text = "点出弹框")
        }
    }

    if (!openAlertDialog.value) {
        return
    }
    AlertDialog(icon = { Icon(imageVector = Icons.Default.Delete, contentDescription = "") },
        title = { Text(text = "确认删除") },
        text = { Text(text = "是否确定删除吗？") },
        onDismissRequest = { onDismissRequest() },
        confirmButton = {
            TextButton(onClick = { confirmClick() }) {
                Text(text = "确认")
            }
        }, dismissButton = {
            TextButton(onClick = { onDismissRequest() }) {
                Text(text = "取消")
            }
        })
}

@Composable
fun MyCornerDialog(
    openAlertDialog: MutableState<Boolean>,
    onDismissRequest: () -> Unit,
    confirmClick: () -> Unit
) {
    Column {
        Button(modifier = Modifier
            .width(120.dp)
            .height(40.dp), onClick = { openAlertDialog.value = !openAlertDialog.value }) {
            Text(text = "点出弹框")
        }
    }

    if (!openAlertDialog.value) {
        return
    }

    Dialog(onDismissRequest = { onDismissRequest() }) {
        Card(
            modifier = Modifier
                .size(200.dp, 300.dp)
                .padding(horizontal = 10.dp), shape = RoundedCornerShape(20.dp)
        ) {
            Column(modifier = Modifier.padding(horizontal = 10.dp)) {
                Text(text = "提醒")
                Image(
                    painter = painterResource(id = R.drawable.ic_launcher_background),
                    contentDescription = "imageDescription",
                    contentScale = ContentScale.Fit,
                    modifier = Modifier
                        .height(160.dp)
                )
                Row {
                    TextButton(onClick = { onDismissRequest() }) {
                        Text(text = "取消")
                    }
                    OutlinedButton(onClick = { confirmClick() }) {
                        Text(text = "确认")
                    }
                }


            }
        }
    }
}

@Composable
fun myBar() {
    //当前进度
    var progress by remember {
        mutableStateOf(0f)
    }
    var loading by remember {
        mutableStateOf(false)
    }
    val scope = rememberCoroutineScope()

    Column {
        Button(onClick = {
            loading = true
            scope.launch {
                getProgress { progress = it }
            }
            //loading=false
        }, enabled = !loading) {
            Text(text = "开始下载")
        }

        if (loading) {
            CircularProgressIndicator(progress = progress, modifier = Modifier.fillMaxWidth())
            if (progress == 1.0f) {
                loading = false
            }
        }
    }
}

suspend fun getProgress(myProgress: (Float) -> Unit) {
    for (i in 0..100) {
        myProgress(i / 100F)
        delay(100)
    }
}

@Composable
fun mySwitch() {
    var check by remember {
        mutableStateOf(false)
    }
    Switch(checked = check, onCheckedChange = {
        check = it
    })
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun myBadge() {
    var state by remember {
        mutableStateOf(false)
    }
    var num by remember {
        mutableStateOf(0)
    }

    Column(modifier = Modifier.padding(20.dp)) {
        BadgedBox(badge = {
            /*if(state){
                Badge(containerColor = Color.Red)}
            }*/
            if (num != 0) {
                Badge(containerColor = Color.Red){
                    Text(text = "$num")
                }
            }
        }
        ) {
            Icon(imageVector = Icons.Filled.Email, contentDescription = "")
        }

        Button(onClick = { /*state=!state*/ ++num }) {
            Text(text = "有内容")
        }

    }
}





